from linkifier import Linkifier
from google.appengine.api import urlfetch
from google.appengine.api import memcache

import logging

from xml.dom import minidom 

class UrlLinker(Linkifier):

	_defaultargs = ('http',)

	@classmethod
	def _link(cls,query,*args):
		return '%s://%s' % (args[0], query)


class GoogleLinker(Linkifier):

	domains = {
			'as':'www.google.as',
			'at':'www.google.at',
			'au':'www.google.com.au',
			'ca':'www.google.ca',
			'cn':'www.google.cn',
			'de':'www.google.de',
			'dk':'www.google.dk',
			'fr':'www.google.fr',
			'hk':'www.google.hk',
			'it':'www.google.it',
			'jp':'www.google.co.jp',
			'lk':'www.google.lk',
			'pl':'www.google.pl',
			'ro':'www.google.ro',
			'ru':'www.google.ru',
			'sh':'www.google.sh',
			'tt':'www.google.tt',
			'uk':'www.google.co.uk',
			'us':'www.google.com',
			'vn':'www.google.com.vn',
			'za':'www.google.co.za',
			}
	
	_defaultargs = ('us',)

	@classmethod
	def _isvalid(cls,query,*args):
		return args[0] in cls.domains

	@classmethod
	def _link(cls,query,*args):
		if args[0] in cls.domains:
			return 'http://%s/search?q=%s' % (cls.domains[args[0]],query)
		else:
			return 'http://www.google.com'








# retrieve from meta.wikimedia.org/wiki/Special:SiteMatrix on 12 Nov 2009
wiki_langs = set([
		'aa','ab','ace','af','ak','als','am','an','ang','ar','arc','arz','as','ast','av','ay','az',
		'ba','bar','bat-smg','bcl','be','be-x-old','bg','bh','bi','bm','bn','bo','bpy','br','bs','bug','bxr',
		'ca','cbk-zam','cdo','ce','ceb','ch','cho','chr','chy','ckb','co','cr','crh','cs','csb','cu','cv','cy',
		'da','de','diq','dk','dsb','dv','dz',
		'ee','el','eml','en','eo','es','et','eu','ext',
		'fa','ff','fi','fiu-vro','fj','fo','fr','frp','fur','fy',
		'ga','gan','gd','gl','glk','gn','got','gu','gv',
		'ha','hak','haw','he','hi','hif','ho','hr','hsb','ht','hu','hy','hz',
		'ia','id','ie','ig','ii','ik','ilo','io','is','it','iu',
		'ja','jbo','jv',
		'ka','kaa','kab','kg','ki','kj','kk','kl','km','kn','ko','kr','ks','ksh','ku','kv','kw','ky',
		'la','lad','lb','lbe','lg','li','lij','lmo','ln','lo','lt','lv',
		'map-bms','mdf','mg','mh','mhr','mi','mk','ml','mn','mo','mr','ms','mt','mus','mwl','my','myv','mzn',
		'na','nah','nan','nb','nds','nds-nl','ne','new','ng','nl','nn','no','nomcom','nov','nrm','nv','ny',
		'oc','om','or','os',
		'pa','pag','pam','pap','pdc','pi','pih','pl','pms','pnb','pnt','ps','pt',
		'qu',
		'rm','rmy','rn','ro','roa-rup','roa-tara','ru','rw',
		'sa','sah','sc','scn','sco','sd','se','sg','sh','si','simple','sk','sl','sm','sn','so','sq','sr','srn','ss','st','stq','su','sv','sw','szl',
		'ta','te','tet','tg','th','ti','tk','tl','tn','to','tokipona','tpi','tr','ts','tt','tum','tw','ty',
		'udm','ug','uk','ur','uz',
		've','vec','vi','vls','vo',
		'wa','war','wo','wuu',
		'xal','xh',
		'yi','yo',
		'za','zea','zh','zh-classic','zh-min-nan','zh-yue','zu',
		])




class WikiLinker(Linkifier):
	languages = wiki_langs
	
	WIKI_API_URL = 'http://%s.wikipedia.org/w/api.php?action=opensearch&search=%s&format=xml'
	WIKI_LINK_URL = 'http://%s.wikipedia.org/w/index.php?title=Special:Search&search=%s'
	WIKI = 'wikipedia'

	_defaultargs = ('en',)

	@classmethod
	def _isvalid(cls,query,*args):
		lang = args[0]
		query = query.replace(' ','%20')
		
		if lang not in cls.languages:
			return False

		cached_hit = memcache.get('%s %s' % (lang, query), namespace=cls.WIKI)
		
		if cached_hit is not None:
			logging.debug("memcache hit %s %s" % (lang, query))
			return cached_hit
		else:
			hit = False
			logging.debug("memcache miss %s %s" % (lang, query))
			try:
				response = urlfetch.fetch(cls.WIKI_API_URL % (lang, query), deadline=2)
				if response.status_code == 200:
					xml = minidom.parseString(response.content)
					hit = bool(xml.getElementsByTagName("Item"))
			except urlfetch.Error:
				return False #fetch error, don't cache result
			
			memcache.set('%s %s' % (lang, query), hit, time=3600*3, namespace=cls.WIKI)
			return hit

	@classmethod
	def _link(cls,query,*args):
		return cls.WIKI_LINK_URL % (args[0],query)


class WiktionaryLinker(WikiLinker):
	WIKI_API_URL = 'http://%s.wiktionary.org/w/api.php?action=opensearch&search=%s&format=xml'
	WIKI_LINK_URL = 'http://%s.wiktionary.org/w/index.php?title=Special:Search&search=%s'
	WIKI = 'wiktionary'

